package com.hrs.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrs.pojo.Employee;
import com.hrs.pojo.User;
import com.hrs.service.EmployeeService;

@Controller
public class EmployeeController {

	@Resource
	private EmployeeService employeeService;
	
//	员工列表加载,分页
	@RequestMapping("/empShow")
	public String empShow(String p,Model model) {
//		设置分页和每页数
		PageHelper.startPage(Integer.parseInt(p),10);
//		查询信息
		List<Employee> empList=employeeService.selectAll();
//		传入pageinfo
		PageInfo<Employee> pageInfo = new PageInfo<>(empList);
//		传递数据
		model.addAttribute("pageInfo", pageInfo);
		return "empShow";
	}
	
	
//	删除员工信息
	@RequestMapping("/empDel")
	public String empDel(String eid,String p) {
		employeeService.deleteEmployeeById(Integer.parseInt(eid));
		return "redirect:/empShow.do?p="+p;
	}
	
	@RequestMapping("empAdd")
	public String empAdd(Employee employee) {
		return "empAdd";
	}
	
//	处理新增
	@RequestMapping("empAddDo")
	public String empAddDo(@Valid Employee employee,BindingResult br,HttpServletRequest request) {
		if (br.hasErrors()) {
			return "empAdd";
		}
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		employee.setCreatedate(sdf.format(new Date()));
		HttpSession session = request.getSession();
		User user=(User) session.getAttribute("USER_SESSION");
		employee.setCreator(user.getLoginname());
		employeeService.insert(employee);
		return "redirect:/empShow.do?p=1";
	}
	
//	修改
	@RequestMapping("empModify")
	public String empModify(String eid,String p,Model model,HttpServletRequest request) {
		
		Employee employee = employeeService.selectEmployeeById(Integer.parseInt(eid));
		
		model.addAttribute("employee", employee);
		
		HttpSession session = request.getSession();
		session.setAttribute("PAGE_NUM", p);
		
		return "empModify";
	}
	
	
//	修改处理
	@RequestMapping("empModifyDo")
	public String empModifyDo(Employee employee,HttpServletRequest request) {
		
		Employee empTemp = employeeService.selectEmployeeById(employee.getEid());
		
		empTemp.setEname(employee.getEname());
		empTemp.setDid(employee.getDid());
		empTemp.setJid(employee.getJid());
		empTemp.setCardno(employee.getCardno());
		empTemp.setAddress(employee.getAddress());
		empTemp.setTel(employee.getTel());
		empTemp.setEmail(employee.getEmail());
		empTemp.setBirthday(employee.getBirthday());
		
		
		
		return "redirect:/empShow.do?p=";
	}
	
//	退出登陆
	@RequestMapping("/exit")
	public String exit(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.invalidate();
		return "redirect:/login.do";
	}
	
}
